package com.weiyin.examination.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.View;

import com.weiyin.examination.R;
import com.weiyin.examination.application.Constant;
import com.weiyin.examination.listener.DialogResultInterface;
import com.weiyin.examination.module.TestModule;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.LogUtil;
import com.weiyin.examination.view.WheelView;

/**
 * 显示备选答案
 *
 * @author njf
 * @date 2017/9/20
 */

public class CourseA1Dialog extends Dialog {

    private TestModule testModule = null;
    private Activity activity;
    private String left = "left", right = "right", center = "center";

    private WheelView recyclerViewLeft, recyclerViewRight, recyclerViewCenter;

    private DialogResultInterface dialogResultInterface;
    LayoutHelper layoutHelper;

    public CourseA1Dialog(@NonNull Activity context) {
        super(context);
    }

    public CourseA1Dialog(@NonNull Activity context, @StyleRes int themeResId, TestModule testModule) {
        super(context, themeResId);
        this.activity = context;
        this.testModule = testModule;
    }

    protected CourseA1Dialog(@NonNull Activity context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public void setDialogResultInterface(DialogResultInterface dialogResultInterface) {
        this.dialogResultInterface = dialogResultInterface;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coursea1_dialog_layout);
        layoutHelper = new LayoutHelper(activity);
        layoutHelper.scaleView(findViewById(R.id.courseA1DialogLayout));
        initView();
    }

    private void initView() {
        if (testModule != null) {
            if (testModule.getSelectionList() != null) {
                if (testModule.getSelectionList().get(0).getLeft() != null && testModule.getSelectionList().get(0).getLeft().size() != 0) {
                    recyclerViewLeft = (WheelView) findViewById(R.id.courseA1DialogRecyclerViewLeft);
                    recyclerViewLeft.setVisibility(View.VISIBLE);
                    recyclerViewLeft.setData(testModule.getSelectionList().get(0).getLeft());
                    recyclerViewLeft.setOnSelectListener(new WheelView.OnSelectListener() {

                        @Override
                        public void endSelect(int id, String text, String answerId) {
                            LogUtil.i("courseA1Dialog endSelect", "id=" + id + " text=" + text);
                            if (dialogResultInterface != null) {
                                if (Constant.EMPTY.equals(text)) {
                                    dialogResultInterface.result(left, "", answerId);
                                } else {
                                    dialogResultInterface.result(left, text, answerId);
                                }
                            }
                        }

                        @Override
                        public void selecting(int id, String text) {

                        }
                    });
                    recyclerViewLeft.setDefault(0);
                    if (dialogResultInterface != null) {
                        if (Constant.EMPTY.equals(testModule.getSelectionList().get(0).getLeft().get(0).getAnswer())) {
                            dialogResultInterface.result(left, "", testModule.getSelectionList().get(0).getLeft().get(0).getId());
                        } else {
                            dialogResultInterface.result(left, testModule.getSelectionList().get(0).getLeft().get(0).getAnswer(), testModule.getSelectionList().get(0).getLeft().get(0).getId());
                        }
                    }
                }
                if (testModule.getSelectionList().get(0).getRight() != null && testModule.getSelectionList().get(0).getRight().size() != 0) {
                    recyclerViewRight = (WheelView) findViewById(R.id.courseA1DialogRecyclerViewRight);
                    recyclerViewRight.setVisibility(View.VISIBLE);
                    recyclerViewRight.setData(testModule.getSelectionList().get(0).getRight());
                    recyclerViewRight.setOnSelectListener(new WheelView.OnSelectListener() {

                        @Override
                        public void endSelect(int id, String text, String answerId) {
                            LogUtil.i("courseA1Dialog endSelect", "id=" + id + " text=" + text);
                            if (dialogResultInterface != null) {
                                if (Constant.EMPTY.equals(text)) {
                                    dialogResultInterface.result(right, "", answerId);
                                } else {
                                    dialogResultInterface.result(right, text, answerId);
                                }
                            }
                        }

                        @Override
                        public void selecting(int id, String text) {
                        }
                    });
                    recyclerViewRight.setDefault(0);
                    if (dialogResultInterface != null) {
                        if (Constant.EMPTY.equals(testModule.getSelectionList().get(0).getRight().get(0).getAnswer())) {
                            dialogResultInterface.result(right, "", testModule.getSelectionList().get(0).getRight().get(0).getId());
                        } else {
                            dialogResultInterface.result(right, testModule.getSelectionList().get(0).getRight().get(0).getAnswer(), testModule.getSelectionList().get(0).getRight().get(0).getId());
                        }
                    }
                }
                if (testModule.getSelectionList().get(0).getCenter() != null && testModule.getSelectionList().get(0).getCenter().size() != 0) {
                    recyclerViewCenter = (WheelView) findViewById(R.id.courseA1DialogRecyclerViewCenter);
                    recyclerViewCenter.setVisibility(View.VISIBLE);
                    recyclerViewCenter.setData(testModule.getSelectionList().get(0).getCenter());
                    recyclerViewCenter.setOnSelectListener(new WheelView.OnSelectListener() {

                        @Override
                        public void endSelect(int id, String text, String answerId) {
                            LogUtil.i("courseA1Dialog endSelect", "id=" + id + " text=" + text);
                            if (dialogResultInterface != null) {
                                if (Constant.EMPTY.equals(text)) {
                                    dialogResultInterface.result(center, "", answerId);
                                } else {
                                    dialogResultInterface.result(center, text, answerId);
                                }
                            }
                        }

                        @Override
                        public void selecting(int id, String text) {
                        }
                    });
                    recyclerViewCenter.setDefault(0);
                    if (dialogResultInterface != null) {
                        if (Constant.EMPTY.equals(testModule.getSelectionList().get(0).getCenter().get(0).getAnswer())) {
                            dialogResultInterface.result(center, "", testModule.getSelectionList().get(0).getCenter().get(0).getId());
                        } else {
                            dialogResultInterface.result(center, testModule.getSelectionList().get(0).getCenter().get(0).getAnswer(), testModule.getSelectionList().get(0).getCenter().get(0).getId());
                        }
                    }
                }
            }
        }
    }
}
